package com.example.json;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new ReadJSONFeedTask().execute("http://210.44.16.158:8080/gallery/dustbin/list.htm?action=show&token=sdssd");
    }

    private class ReadJSONFeedTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            URL url = null;
            try{
                url = new URL(strings[0]);
            } catch (MalformedURLException e){
                e.printStackTrace();
            }

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }

            return readJSONFeed(url);
        }

        @Override
        protected void onPostExecute(String s) {
            try {
                JSONArray jsonArray = new JSONArray(s);
                Log.i("JSON", "Number of surveys in feed: " + jsonArray.length());

                for(int i = 0; i < jsonArray.length(); i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Toast.makeText(getBaseContext(), jsonObject.getInt("id") + "-" + jsonObject.getString("code") + "-" + jsonObject.getString("location") + "-" + jsonObject.getDouble("longitude") + "-"+ jsonObject.getDouble("latiude") + "-"+ jsonObject.getDouble("capacity"), Toast.LENGTH_LONG).show();
                }

                startActivity(new Intent("com.example.json.ImageActivity"));
            } catch (Exception e){
                e.printStackTrace();
            }
        }


    }

    public String readJSONFeed(URL url){
        StringBuilder stringBuilder = new StringBuilder();

        HttpURLConnection urlConnection = null;
        try {

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");

            if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
                BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

                String line;

                while ((line = reader.readLine()) != null){
                    stringBuilder.append(line);
                }
            } else {
                Log.e("JSON", "Failed to download file");
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }

        return stringBuilder.toString();
    }
}
